package org.cytoscape.myapp.my_cyaction_app.internal;


import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.cytoscape.group.CyGroup;
import org.cytoscape.group.CyGroupFactory;
import org.cytoscape.group.CyGroupManager;
import org.cytoscape.model.CyNetwork;
import org.cytoscape.model.CyNode;
import org.cytoscape.model.CyRow;
import org.cytoscape.model.CyTable;

public class CySimpleSorter  extends CySorter{

	private Object value;
	
	public CySimpleSorter(CyGroupFactory FactoryGroup, CyGroupManager ManagerGroup, String NameAttribute, Object value){
		super(FactoryGroup,ManagerGroup, NameAttribute);
		this.value = value;		
	}

	@Override
	public CyGroup GetCyGroupClasificated(CyNetwork network) {

		CyGroup grupo = FactoryGroup.createGroup(network, true);
		CyTable table = network.getDefaultNetworkTable();
		final Collection<CyRow> matchingRows = table.getMatchingRows(this.GetNameAttribute(), value);
		List<CyNode> nodes = new ArrayList<CyNode>();
		final String primaryKeyColname = table.getPrimaryKey().getName();
        for (final CyRow row : matchingRows)
        {
            final Long nodeId = row.get(primaryKeyColname, Long.class);
            if (nodeId == null)
                continue;
            final CyNode node = network.getNode(nodeId);
            if (node == null)
                continue;
            nodes.add(node);
        }
		grupo.addNodes(nodes);
		
		return grupo;
	}
}

